$(function () {
    getPage(1)
})

//翻页
function getPage(pn) {

    $.get("/j2ee/business/getGallery.do", {}, function (data) {
        var jdata = eval("(" + data + ")");
        var list = jdata.list;
        var dataCount = list.length;//总数据条数

        var pageSize = 9;//每页显示条数
        var pageCount = Math.ceil(dataCount / pageSize);//总页数
        if (pn == 0 || pn > pageCount) {
            return;
        }
        $("#testui").empty();
        var ul = $("#testui");
        //console.log(pageCount+"..."+pn)
        paintPage(pageCount, pn);   //绘制页码
        var startPage = pageSize * (pn - 1);
        if (pageCount == 1) {     // 当只有一页时
            for (var j = 0; j < dataCount; j++) {
                var x = j % 9 + 1;


                var stars = "";
                //评分数
                for(var m=0;m<list[j].stars-0.5;m++){
                    stars+="<img src=\"../img/love_red.png\" width=\"15\" height=\"15\">";
                }
                for(var n=0;n<5-list[j].stars;n++){
                    stars+="<img src=\"../img/love_gray.png\" width=\"15\" height=\"15\">";
                }
                var e = "<div class=\"grid_4\"><div class=\"gall_block\"><div class=\"maxheight\"><a href=\"/j2ee/user/showResInfo.do?r_id="+ list[j].id+"\" class=\"gall_item\"><img src=\"../images/page2_img" + x + ".jpg\"></a><div class=\"gall_bot\"><div class=\"text1\"><a href=\"#\">" + list[j].name + "</a></div><p class=\"desc\">"+stars+"&nbsp;"+list[j].reviewCount+"条点评  |  人均 ￥"+list[j].p_p_p+"</p><p class=\"desc\">"+list[j].cuisine+" | "+list[j].address+"</p><p class=\"desc\">推荐菜："+list[j].recomDishes+"</p><p class=\"like\">口味"+list[j].tasteStars+" 环境"+list[j].environStars+" 服务"+list[j].serviceStars+"</p></div></div></div></div>";
                ulul.append(e);
                if (x == 3 || x == 6 || x == 9) {
                    var h = "<div class=\"clear sep__1\"></div>";
                    ul.append(h);
                }
            }
        } else {      // 当超过一页时
            var endPage = pn < pageCount ? pageSize * pn : dataCount;
            for (var j = startPage; j < endPage; j++) {
                //alert(list[j].name);
                var x = j % 9 + 1;
                var stars = "";
                //评分数
                for(var m=0;m<list[j].stars-0.5;m++){
                    stars+="<img src=\"../img/love_red.png\" width=\"15\" height=\"15\">";
                }
                for(var n=0;n<5-list[j].stars;n++){
                    stars+="<img src=\"../img/love_gray.png\" width=\"15\" height=\"15\">";
                }
                var e = "<div class=\"grid_4\"><div class=\"gall_block\"><div class=\"maxheight\"><a href=\"/j2ee/user/showResInfo.do?r_id="+ list[j].id+"\" class=\"gall_item\"><img src=\" "+list[j].image+"\" onerror='javascript:this.src=\"../images/img_1.jpg\"'></a><div class=\"gall_bot\"><div class=\"text1\"><a href=\"#\">" + list[j].name + "</a></div><p class=\"desc\">"+stars+"&nbsp;"+list[j].reviewCount+"条点评  |  人均 ￥"+list[j].p_p_p+"</p><p class=\"desc\">"+list[j].cuisine+" | "+list[j].address+"</p><p class=\"desc\">推荐菜："+list[j].recomDishes+"</p><p class=\"like\">口味"+list[j].tasteStars+" 环境"+list[j].environStars+" 服务"+list[j].serviceStars+"</p></div></div></div></div>";
                ul.append(e);
                if (x == 3 || x == 6 || x == 9) {
                    var h = "<div class=\"clear sep__1\"></div>";
                    ul.append(h);
                }

            }
        }
        ul.show();
    });
};

//绘制页码
function paintPage(number, currNum)  //number 总页数,currNum 当前页
{
    var pageUl = $(".fenye");
    pageUl.empty();
    var ulDetail = "";

    if (number == 1) {
        ulDetail = "<li class=\"prev\"><a href=\"javascript:void(0)\">prev</a></li>" +
            "<li class=\"numb choose\"><a href=\"javascript:getPage(1)\">1</a></li>" +
            "<li class=\"next\"><a href=\"javascript:void(0)\">next</a></li>";
    } else if (number == 2) {
        ulDetail = "<li class=\"prev\"><a href=\"javascript:getPage(1)\">prev</a></li>" +
            "<li class=\"numb" + choosele(currNum, 1) + "\"><a href=\"javascript:getPage(1)\">1</a></li>" +
            "<li class=\"numb" + choosele(currNum, 2) + "\"><a href=\"javascript:getPage(2)\">2</a></li>" +
            "<li class=\"next\"><a href=\"javascript:getPage(2)\">next</a></li>";
    } else if (number == 3) {
        ulDetail = "<li class=\"prev\"><a href=\"javascript:getPage(" + parseInt(currNum - 1) + ")\">prev</a></li>" +
            "<li class=\"numb" + choosele(currNum, 1) + "\"><a href=\"javascript:getPage(1)\">1</a></li>" +
            "<li class=\"numb" + choosele(currNum, 2) + "\"><a href=\"javascript:getPage(2)\">2</a></li>" +
            "<li class=\"numb" + choosele(currNum, 3) + "\"><a href=\"javascript:getPage(3)\">3</a></li>" +
            "<li class=\"next\"><a href=\"javascript:getPage(" + parseInt(currNum + 1) + ")\">next</a></li>";
    } else if (number == currNum && currNum > 3) {
        ulDetail = "<li class=\"prev\"><a href=\"javascript:getPage(" + parseInt(currNum - 1) + ")\">prev</a></li>" +
            "<li class=\"numb\"><a href=\"javascript:getPage(" + parseInt(currNum - 2) + ")\">" + parseInt(currNum - 2) + "</a></li>" +
            "<li class=\"numb\"><a href=\"javascript:getPage(" + parseInt(currNum - 1) + ")\">" + parseInt(currNum - 1) + "</a></li>" +
            "<li class=\"numb choose\"><a href=\"javascript:getPage(" + currNum + ")\">" + currNum + "</a></li>" +
            "<li class=\"next\"><a href=\"javascript:getPage(" + currNum + ")\">next</a></li>";
    } else if (currNum == 1 && number > 3) {
        ulDetail = "<li class=\"prev\"><a href=\"javascript:void(0)\">prev</a></li>" +
            "<li class=\"numb choose\"><a href=\"javascript:void(0)\">1</a></li>" +
            "<li class=\"numb\"><a href=\"javascript:getPage(2)\">2</a></li>" +
            "<li class=\"numb\"><a href=\"javascript:getPage(3)\">3</a></li>" +
            "<li class=\"next\"><a href=\"javascript:getPage(2)\">next</a></li>";
    } else {
        ulDetail = "<li class=\"prev\"><a href=\"javascript:getPage(" + parseInt(currNum - 1) + ")\">prev</a></li>" +
            "<li class=\"numb\"><a href=\"javascript:getPage(" + parseInt(currNum - 1) + ")\">" + parseInt(currNum - 1) + "</a></li>" +
            "<li class=\"numb choose\"><a href=\"javascript:getPage(" + currNum + ")\">" + currNum + "</a></li>" +
            "<li class=\"numb\"><a href=\"javascript:getPage(" + parseInt(currNum + 1) + ")\">" + parseInt(currNum + 1) + "</a></li>" +
            "<li class=\"next\"><a href=\"javascript:getPage(" + parseInt(currNum + 1) + ")\">next</a></li>";
    }

    $(".fenye").append(ulDetail);

};

function choosele(num, cur) {
    if (num == cur) {
        return " choose";
    } else {
        return "";
    }
};

$(document).ready(function () {

    $().UItoTop({easingType: 'easeOutQuart'});
    $('#stuck_container').tmStickUp({});
    $('.gallery .gall_item').touchTouch();

});